import os
import sys
import django

# 设置Django环境，使用正确的设置模块名称
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'config.settings')
django.setup()

from cases.models import TestCase, Version
from projects.models import Project

print("开始添加使用新设计方法的测试用例...")

# 获取第一个项目和版本作为示例
project = Project.objects.first()
version = Version.objects.first()

if not project or not version:
    print("找不到项目或版本数据，请先运行add_sample_data.py")
    exit(1)

# 使用因果图法的测试用例
causal_case, created = TestCase.objects.get_or_create(
    case_id="TEST-NEW-001",
    defaults={
        "version": version,
        "title": "用户登录验证因果图测试",
        "description": "使用因果图法测试用户登录时多个条件的组合影响",
        "precondition": "无",
        "steps": "1. 准备不同的用户名密码组合\n2. 尝试登录系统\n3. 记录登录结果",
        "expected_result": "系统能够正确处理各种条件组合，符合预期逻辑",
        "actual_result": "",
        "status": "reviewed",
        "priority": "high",
        "created_by": "admin",
        "module": "用户管理",
        "test_method": "cause_effect"
    }
)
if created:
    print(f"已添加因果图法测试用例: {causal_case.title}")
else:
    print(f"因果图法测试用例已存在: {causal_case.title}")

# 使用正交法的测试用例
orthogonal_case, created = TestCase.objects.get_or_create(
    case_id="TEST-NEW-002",
    defaults={
        "version": version,
        "title": "表单输入正交测试",
        "description": "使用正交法测试表单不同输入字段的组合",
        "precondition": "系统中有表单功能",
        "steps": "1. 根据正交表选择不同的输入值组合\n2. 提交表单\n3. 验证表单处理结果",
        "expected_result": "所有组合都能正常工作，覆盖主要场景",
        "actual_result": "",
        "status": "draft",
        "priority": "medium",
        "created_by": "admin",
        "module": "表单处理",
        "test_method": "orthogonal"
    }
)
if created:
    print(f"已添加正交法测试用例: {orthogonal_case.title}")
else:
    print(f"正交法测试用例已存在: {orthogonal_case.title}")

# 使用流程图法的测试用例
flowchart_case, created = TestCase.objects.get_or_create(
    case_id="TEST-NEW-003",
    defaults={
        "version": version,
        "title": "用户注销流程测试",
        "description": "按照流程图验证完整的用户注销流程",
        "precondition": "用户已登录系统",
        "steps": "1. 点击用户头像\n2. 选择注销选项\n3. 确认注销\n4. 验证注销结果",
        "expected_result": "用户能够成功注销，系统状态正确",
        "actual_result": "",
        "status": "reviewed",
        "priority": "high",
        "created_by": "admin",
        "module": "用户管理",
        "test_method": "flowchart"
    }
)
if created:
    print(f"已添加流程图法测试用例: {flowchart_case.title}")
else:
    print(f"流程图法测试用例已存在: {flowchart_case.title}")

print("使用新设计方法的测试用例添加完成！")